package com.sjzsqjy.asdtiang.util;

/**
 * <p>Title: 利民商一卡通消费系统</p>
 *
 * <p>Description: 利民商一卡通消费系统</p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: 利民商一卡通</p>
 *
 * @author asdtiang
 * @version 1.0
 */
/**
 *
 * @author Administrator
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *文件读写类
 * @author asdtiang
 */
public class MyFile {

    private String url;
    private BufferedWriter writer;
    private Writer w;
    private BufferedReader reader;
    private Reader r;
    private File f;
    /**
    *
    * @param url 文件的路径
    */
    public  MyFile(String url) {

        this.url = url;
        f = new File(url);

    }
    public MyFile(File f){
        this.f=f;
    }
    /**
    *
    * 获取BufferedWriter类的实例
    */
    public BufferedWriter getWriter() {

        if (!f.exists() || !f.isFile()) {
            try {
                f.createNewFile();

            } catch (IOException ex) {
                Logger.getLogger(MyFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            writer = new BufferedWriter(new FileWriter(f));
        } catch (IOException ex) {
            Logger.getLogger(MyFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        return writer;
    }
    /**
    *
    * 获取BufferedReader类的实例
    */
    public BufferedReader getReader() {

        if (!f.exists() || !f.isFile()) {
            return null;
        }
        try {
            reader = new BufferedReader(new FileReader(f));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MyFile.class.getName()).log(Level.SEVERE, null, ex);
        }

        return reader;

    }
    /**
    *
    * 获取文件的绝对路径
    */
    public String getPath(){
        try {
                        return f.getCanonicalPath();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return f.getAbsolutePath();
    }
    /**
    *
    * 获取File类的实例
    */
    public File getFile(){
        return f;

    }
    /**
    *
    * 关闭I/O流
    */
    public void close(){
        if(reader!=null){
                try {
                                reader.close();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
        }
        if(writer!=null){
                try {
                                writer.close();

                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
        }

    }
    public static List getListFromFlie(String fileName,int length,String charSet){
    	
    	 File file=new File(fileName);
    	 List list=new ArrayList();
    	 try {
 	    	InputStreamReader inReader =new InputStreamReader(new FileInputStream(file), charSet);
 	    	char[] ch=new char[length];
 	    	String s="";
 	    	inReader.read(ch);
 			for(int i=0;i<ch.length;i++){
 	    		if(ch[i]==13){
 	    			list.add(s);
 	    			s="";
 	    		}
 	    		else{
 	    			s+=ch[i];
 	    		}
 	    	}
 			System.out.println(list);
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 		return list;

    	
    }
}
